﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Web;

namespace Liguo.AppCode
{
    public class EMail : IDisposable
    {
        private static String urlBase = "http://115.154.71.224:8086/Store/";
        private static String mainPage = urlBase + "Store.aspx";
        private static String helpPage = urlBase + "Help.aspx";
        private static String loginPage = urlBase + "Port.aspx?mod=Login";

        private String fromAddr;
        private String smtpHost;
        private String serverPwd;
        private int smtpPort;
        private String serverName;
        private SmtpClient client;
        private Attachment att;

        public EMail(String from = "liguoserver@163.com", String smtp = "smtp.163.com", String pwd = "liguoshe", int port = 25, String name = "liguoserver")
        {
            fromAddr = from;
            smtpHost = smtp;
            serverPwd = pwd;
            smtpPort = port;
            serverName = name;
            InitSmtpClient();
        }

        private void InitSmtpClient()
        {
            client = new SmtpClient(smtpHost, smtpPort);
            client.UseDefaultCredentials = false;
            client.Credentials = new NetworkCredential(serverName, serverPwd);
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            String logoPath = HttpRuntime.AppDomainAppPath.ToString() + "\\res\\liguo_big_logo200x80.png";
            att = new Attachment(logoPath);
            att.ContentId = "logo";
        }

        public bool SendActiveMessage(String userName, String mailBox, String activeCode)
        {
            MailMessage mail = new MailMessage();
            mail.From = new MailAddress(fromAddr);
            mail.To.Add(mailBox);
            String activeUrl = urlBase + "Active.aspx?uid=" + userName + "&key=" + activeCode; ;
            mail.Subject = "请尽快激活您的利果大百货账号！";
            mail.BodyEncoding = System.Text.Encoding.Default;
            mail.Body = ActiveMessage(userName, activeUrl);
            mail.Attachments.Add(att);
            mail.IsBodyHtml = true;
            try
            {
                client.Send(mail);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool SendResetPwdMessage(String userName, String mailBox, String resetPwdCode)
        {
            MailMessage mail = new MailMessage();
            mail.From = new MailAddress(fromAddr);
            mail.To.Add(mailBox);
            mail.BodyEncoding = System.Text.Encoding.Default;
            mail.IsBodyHtml = true;
            mail.Attachments.Add(att);
            String resetPwdUrl = urlBase + "GetBackPassword.aspx?uid=" + userName + "&key=" + resetPwdCode; ;
            mail.Subject = "请尽快重置您的利果大百货账号密码！";
            mail.Body = ResetPwdMessage(userName, resetPwdUrl);
            try
            {
                client.Send(mail);
                return true;
            }
            catch
            {
                return false;
            }
        }

        private String ActiveMessage(String userName, String activeUrl)
        {
            return UrlMessage(userName, activeUrl, 0);
        }

        private String ResetPwdMessage(String userName, String resetPwdUrl)
        {
            return UrlMessage(userName, resetPwdUrl, 1);
        }

        private String UrlMessage(String userName, String url, int mod)
        {
            String[] tips = new String[2];
            switch (mod)
            {
                case 0:
                    //激活账户
                    tips[0] = "您已申请开通利果大百货账号，请立即激活！";
                    tips[1] = "点此立即激活";
                    break;
                case 1:
                    //找回密码
                    tips[0] = "您已申请重置您的密码，请立即重置！";
                    tips[1] = "点此重置密码";
                    break;
            }
            StringBuilder message = new StringBuilder();
            message.Append(MessageHead());
            #region 邮件主要内容
            message.Append("<div style=\"line-height:1.5;font-size:14px;margin-bottom:25px;color:#4d4d4d;\">");
            message.Append("<strong style=\"display:block;margin-bottom:15px;\">亲爱的会员：" + userName + "您好！</strong>");
            message.Append("<p>" + tips[0] + "</p>");
            message.Append("</div>");
            message.Append("<div style=\"margin-bottom:30px;\">");
            message.Append("<strong style=\"display:block;margin-bottom:20px;font-size:14px;\">");
            message.Append("<a target=\"_blank\" style=\"color:#f60;\" href=\"" + url + "\">" + tips[1] + "</a>");
            message.Append("</strong>");
            message.Append("<p style=\"color:#666;\">");
            message.Append("<small style=\"display:block;font-size:12px;margin-bottom:5px;\">");
            message.Append("如果上述文字点击无效，请把下面网页地址复制到浏览器地址栏中打开：");
            message.Append("</small>");
            message.Append("<span style=\"color:#666;\">" + url + "</span>");
            message.Append("</p>");
            message.Append("</div>");
            message.Append("</div>");
            #endregion
            message.Append(MessageTail());
            return message.ToString();
        }

        private String MessageHead()
        {
            StringBuilder head = new StringBuilder();
            head.Append("<!DOCTYPE html>");
            head.Append("<html>");
            head.Append("<head>");
            head.Append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />");
            head.Append("<style type=\"text/css\">");
            head.Append("&lt;!--");
            head.Append("body a{color:#07f;}");
            head.Append("body a:hover{text-decoration:underline;}");
            head.Append("body p,body div table tr td{line-height:1.5;}");
            head.Append("body div table tr td{font-size:14px;}");
            head.Append("body div{font-size:12px;}");
            head.Append("--&gt;");
            head.Append("</style>");
            head.Append("</head>");
            head.Append("<body>");
            head.Append("<table width=\"700\" border=\"0\" align=\"center\" cellspacing=\"0\" style=\"width:700px;\">");
            head.Append("<tbody>");
            head.Append("<tr>");
            head.Append("<td>");
            head.Append("<div style=\"width:700px;margin:0 auto;border-bottom:1px solid #ccc;margin-bottom:30px;\">");
            head.Append("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"700\" height=\"80\" style=\"font:12px Tahoma, Arial, 宋体;\">");
            head.Append("<tbody>");
            head.Append("<tr>");
            head.Append("<td width=\"210\">");
            head.Append("<a target=\"_blank\" href=\"" + mainPage + "\">");
            head.Append("<img src=\"cid:logo\" width=\"200\" height=\"80\" border=\"0\">");
            head.Append("</a>");
            head.Append("</td>");
            head.Append("<td width=\"490\" align=\"right\" valign=\"bottom\" style=\"padding-bottom:10px;\">");
            head.Append("<a target=\"_blank\" style=\"color:#07f;text-decoration:none;font-size:12px;\" href=\"" + loginPage + "\">登录</a>");
            head.Append(" | ");
            head.Append("<a target=\"_blank\" style=\"color:#07f;text-decoration:none;padding-right:5px;font-size:12px;\" href=\"" + helpPage + "\">帮助中心</a>");
            head.Append("</td>");
            head.Append("</tr>");
            head.Append("</tbody>");
            head.Append("</table>");
            head.Append("</div>");
            head.Append("<div style=\"width:680px;padding:0 10px;margin:0 auto;\">");
            return head.ToString();
        }

        private String MessageTail()
        {
            StringBuilder tail = new StringBuilder();
            tail.Append("<div style=\"width:700px;margin:0 auto;\">");
            tail.Append("<div style=\"padding:0 10px;width:285px;margin-bottom:30px;\">");
            tail.Append("<p style=\"padding-top:10px;border-top:1px dashed #ccc;\">");
            tail.Append("<a target=\"_blank\" style=\"color:#07f;text-decoration:none;\" href=\"" + helpPage + "\">更多帮助</a>");
            tail.Append("</p>");
            tail.Append("</div>");
            tail.Append("<div style=\"padding:10px 10px 0;border-top:1px solid #ccc;color:#999;margin-bottom:20px;line-height:1.3em;font-size:12px;\">");
            tail.Append("<p style=\"margin-bottom:15px;\">此为系统邮件，请勿回复<br>请保管好您的邮箱，避免利果大百货账户被他人盗用</p>");
            tail.Append("<p>如有任何疑问，可查看 ");
            tail.Append("<a target=\"_blank\" style=\"color:#666;text-decoration:none;\" href=\"" + helpPage + "\">利果大百货相关规则</a>");
            tail.Append("，利果大百货网站访问 ");
            tail.Append("<a target=\"_blank\" style=\"color:#666;text-decoration:none;\" href=\""+mainPage+"\">" + mainPage + "</a>");
            tail.Append("<br>Copyright 利果大百货 2008-2013 All Right Reserved");
            tail.Append("</p>");
            tail.Append("</div>");
            tail.Append("</div>");
            tail.Append("</td>");
            tail.Append("</tr>");
            tail.Append("</tbody>");
            tail.Append("</table>");
            tail.Append("</body>");
            tail.Append("</html>");
            return tail.ToString();
        }

        public void Dispose()
        {
            client.Dispose();
        }

        ~EMail()
        {
            client.Dispose();
        }
    }
}